function schedule_refresh(frame, url, refresh) {
    if (refresh > 0) {
        t = setTimeout(function() {
            $(frame).load(url);
            schedule_refresh(frame, url, refresh);
        }, refresh * 1000);
        $(frame).attr('timeout', t);
    }
}

$(document).ready(function() {
    $("a.toggle").click(function() {
        var text = $(this).text();
        var td = $(this).parent('td');
        var frame = $(td).find('div.frame-div');

        var refresh = $(td).find('input[name="refresh"]').val();
        if (refresh == '') {
            refresh = 30;
        } else {
            refresh = parseInt(refresh);
        }

        var line = $(td).find('input[name="line"]').val();
        if (line == '') {
            line = 20;
        } else {
            line = parseInt(line);
        }

        var target = $(td).find('input[name="target"]').val();

        // No matter what, we need to disable next refresh if any.
        if ($(frame).attr('timeout') != undefined) {
            clearTimeout($(frame).attr('timeout'));
        }


        // alert(sprintf("%s, %d %d", target, refresh, line));
        if (text == 'show') {
            $(frame).fadeIn('slow', function() {
                var url = sprintf('tail.php?target=%s&line=%d',
                                  target, line);
                $(frame).load(url);
                schedule_refresh(frame, url, refresh);

            });
        } else {
            $(frame).fadeOut('slow');
               $(frame).html('');
        }
       })
});